import { StackActions, NavigationActions, deepCopy, Storage } from "../../../utils"
import { request } from '../../../utils/NetUtil'
export default {
    namespace: 'login',
    state: {
      login: false,
      loading: true,
      fetching: false,
    },
    reducers: {
      updateState(state, { payload }) {
        return { ...state, ...payload }
      },
    },
    effects: {
      *loadStorage(action, { call, put }) {
        const login = yield call(Storage.get, 'login', false)
        yield put({
          type:'updateState',
          payload:{
            login, fetching: false
          }
       })
      },
      *login({ payload }, { call, put }) {
        yield put({
           type:'updateState',
           payload:{
            fetching: true
           }
        })
        const login = yield call(request,{ url: '/block/home/app/banner', method: 'GET' })
        if (login) {
          yield put(NavigationActions.back())
        }
        yield put({
          type:'updateState',
          payload:{
            login, fetching: false
          }
       })
        Storage.set('login', login)
      },
      *logout(action, { call, put }) {
        yield call(Storage.set, 'login', false)
        yield put({
          type:'updateState',
          payload:{
            login, fetching: false
          }
       })
      },
    },
    subscriptions: {
      setup({ dispatch }) {
        dispatch({ type: 'loadStorage' })
      },
    },
  }